package com.mytijian.task.model;

import javax.validation.constraints.NotNull;

public class TaskValue {
    private static String split = "_";
    private Integer processingSubTaskId;
    @NotNull
    private Integer taskStuts;
    /**
     * set processSubTaskid
     * try {                              定时任务启动，同步远程数据到mysql update tb_sub_task set status = #{status} where status = 0 and id <=complate
     *      processing
     * }catch() {
     *     seterror to mysql 失败的信息已经写入mysql
     * }
     * processComplete
     */
    private Integer completeSubTaskId;
    private Boolean needFailover;
    public TaskValue() {

    }

    public Integer getCompleteSubTaskId() {
        return completeSubTaskId;
    }

    public void setCompleteSubTaskId(Integer completeSubTaskId) {
        this.completeSubTaskId = completeSubTaskId;
    }

    public Boolean getNeedFailover() {
        return needFailover;
    }

    public void setNeedFailover(Boolean needFailover) {
        this.needFailover = needFailover;
    }

    public Integer getTaskStuts() {
        return taskStuts;
    }

    public void setTaskStuts(Integer taskStuts) {
        this.taskStuts = taskStuts;
    }

    public Integer getProcessingSubTaskId() {
        return processingSubTaskId;
    }

    public void setProcessingSubTaskId(Integer processingSubTaskId) {
        this.processingSubTaskId = processingSubTaskId;
    }

    public TaskValue(@NotNull Integer processingSubTaskId, @NotNull Integer taskStuts,@NotNull Boolean needFailover) {
        this.processingSubTaskId = processingSubTaskId;
        this.taskStuts = taskStuts;
        this.needFailover = needFailover;
    }
    public  String getValue() {
        StringBuilder builder = new StringBuilder();
        builder.append(processingSubTaskId);
        builder.append(split);
        builder.append(taskStuts);
        builder.append(split);
        builder.append(needFailover);
        builder.append(split);
        builder.append(completeSubTaskId);
        return builder.toString();
    }
    public static TaskValue build(String value) {
        String[] values = value.split(split);
        TaskValue taskValue = new TaskValue();
        taskValue.setProcessingSubTaskId(getInteger(values[0]));
        taskValue.setTaskStuts(getInteger(values[1]));
        taskValue.setNeedFailover(getBoolean(values[2]));
        taskValue.setCompleteSubTaskId(getInteger(values[3]));
        return taskValue;
    }
    private static Integer getInteger(String value) {
        if ("null".equals(value)) {
            return null;
        }
        return Integer.parseInt(value);
    }
    private static Boolean getBoolean(String value) {
        if ("null".equals(value)) {
            return null;
        }
        return Boolean.valueOf(value);
    }

    @Override
    public String toString() {
        return getValue();
    }

    public static void main(String[] args) {
        TaskValue taskValue = new TaskValue();
        StringBuilder builder = new StringBuilder();
        builder.append(taskValue.getProcessingSubTaskId()).append("_").append(1);
        String[] strings = builder.toString().split("_");
        for (String s : strings) {
            System.out.println(Integer.parseInt(s));
        }
    }
}
